from src.database.dao.video_dao import VideoDAO
from src.database.dao.generated_video_dao import GeneratedVideoDAO
from src.database.dao.upload_file_dao import UploadedFileDAO
from src.database.dao.video_vector_mapping_dao import VideoVectorMappingDAO
from src.database.dao.video_geraration_config_dao import VideoGenerationConfigDAO
from src.database.dao.monitored_user_dao import MonitoredUserDAO
from src.database.manager import DBManager

class DAOFactory:
    """DAO工厂单例，管理所有DAO实例和数据库会话"""
    
    _instance = None
    
    @classmethod
    def get_instance(cls):
        if cls._instance is None:
            cls._instance = DAOFactory()
        return cls._instance
    
    def __init__(self):
        # 防止直接初始化
        if self._instance is not None:
            raise RuntimeError("请使用 get_instance() 获取 DAOFactory 实例")
        
        # 共享会话
        self.session = DBManager().get_session()
        
        # 创建DAO实例
        self._video_dao = None
        self._generated_video_dao = None
        self._uploaded_file_dao = None
        self._video_vector_mapping_dao = None
        self._video_generation_config_dao = None
        self._monitored_user_dao = None
    
    def get_video_dao(self):
        """获取VideoDAO实例"""
        if self._video_dao is None:
            self._video_dao = VideoDAO(session=self.session)
        return self._video_dao
    
    def get_generated_video_dao(self):
        """获取GeneratedVideoDAO实例"""
        if self._generated_video_dao is None:
            self._generated_video_dao = GeneratedVideoDAO(session=self.session)
        return self._generated_video_dao
    
    def get_uploaded_file_dao(self):
        """获取UploadedFileDAO实例"""
        if self._uploaded_file_dao is None:
            self._uploaded_file_dao = UploadedFileDAO(db=self.session)
        return self._uploaded_file_dao
        
    def get_video_vector_mapping_dao(self):
        """获取VideoVectorMappingDAO实例"""
        if self._video_vector_mapping_dao is None:
            self._video_vector_mapping_dao = VideoVectorMappingDAO(session=self.session)
        return self._video_vector_mapping_dao
        
    def get_video_generation_config_dao(self):
        """获取VideoGenerationConfigDAO实例"""
        if self._video_generation_config_dao is None:
            self._video_generation_config_dao = VideoGenerationConfigDAO(session=self.session)
        return self._video_generation_config_dao
        
    def get_monitored_user_dao(self):
        """获取MonitoredUserDAO实例"""
        if self._monitored_user_dao is None:
            self._monitored_user_dao = MonitoredUserDAO(db_session=self.session)
        return self._monitored_user_dao
    
    def close_session(self):
        """关闭当前会话"""
        if self.session:
            self.session.close()
            # 重置状态以便下次使用时创建新会话
            self.session = DBManager().get_session()
            self._video_dao = None
            self._generated_video_dao = None
            self._uploaded_file_dao = None
            self._video_vector_mapping_dao = None
            self._video_generation_config_dao = None
            self._monitored_user_dao = None
    
    def refresh_session(self):
        """刷新当前会话状态"""
        self.close_session()